﻿using System;
using System.Diagnostics;
using System.Linq;
using System.Windows.Forms;
using Timer = System.Timers.Timer;
namespace osu_BeatmapAutoLoader
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        public void Info_Notification(string title, string text)
        {
            notification.ShowBalloonTip(2000, title, text, ToolTipIcon.Info);
        }

        public void Error_Notification(string title, string text)
        {
            notification.ShowBalloonTip(2000, title, text, ToolTipIcon.Error);
        }

        public void AddLog(string info)
        {
            rtb_log.BeginInvoke(new EventHandler((sender, args) => rtb_log.AppendText(info + "\n")));
        }

        private void btn_Start_Click(object sender, EventArgs e)
        {
            if (!Process.GetProcessesByName("osu!").Any() && Program.OsuPatch != null)
            {
                _timer.Stop();
                Process.Start(Program.OsuPatch);
            }
            if (Injector.TryInject("osu!"))
            {
                btn_Start.Enabled = false;
                Visible = false;
                EventReg();
                DownloadManagement.Run();
            }
        }

        private void EventReg()
        {
            InjectInterface.InjectFailed += InjectInterface_InjectFailed;
        }

        private void InjectInterface_InjectFailed()
        {
            notification.ShowBalloonTip(3000, "Inject Error!", "osu!BeatmapAutoLoader stoped", ToolTipIcon.Error);
            BeginInvoke(new EventHandler(btn_Start_Click));
            btn_Start.Enabled = true;
            BeginInvoke(new EventHandler((sender, args) =>
            {
                Visible = true;
                TopMost = true;
            }));
        }

        private void notification_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                Visible = !Visible;
            }
        }

        private void notification_BalloonTipClicked(object sender, EventArgs e)
        {
        }

        public void AddMenuItem(string setId, string title, EventHandler onClick)
        {
            BeginInvoke(new EventHandler((sender, args) => menu.Items.Add("Cancel " + title, null, onClick).Name = setId));
        }

        public void RemoveMenuItem(string setId)
        {
            BeginInvoke(new EventHandler((sender, args) => menu.Items.RemoveByKey(setId)));
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            if (Program.OsuPatch != null)
            {
                AddLog("osu!Patch:" + Program.OsuPatch);
            }
            else
            {
                AddLog("Cannot find osu! in default directory!\nPlease run osu! manually");
            }
            _timer = new Timer(1000);
            _timer.Elapsed += (o, args) =>
            {
                if (Process.GetProcessesByName("osu!").Any())
                {
                    _timer.Stop();
                    BeginInvoke(new EventHandler(btn_Start_Click));
                }
                _timer.Start();
            };
        }
    }
}